package com.myjob.handlerImpl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myjob.common.DubboConfig;
import com.myjob.config.MjServiceName;
import com.myjob.entity.Company;
import com.myjob.enums.CompanyEnums;
import com.myjob.enums.UserEnums;
import com.myjob.handler.CompanyHandler;
import com.myjob.mapper.CompanyMapper;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Chen Yuan
 * @create 2021-01-28  15:02
 */
@Slf4j
@Component
@Service(group = MjServiceName.COMPANY,
		retries = DubboConfig.RETRIES,
		timeout = DubboConfig.TIME_OUT,
		version = DubboConfig.VERSION)
public class CompanyHandlerImpl implements CompanyHandler {
	@Autowired
	private CompanyMapper companyMapper;

	@Override
	public Company selectOneCompany(String companyId) {
		log.info("[Dubbo] - 被远程调用 -> CompanyHandler -> selectOneCompany("+companyId+")");
		Company company = null;
		if (UtilValidate.isNotEmpty(companyId)){
			QueryWrapper<Company> wrapper = new QueryWrapper<>();
			wrapper.eq("company_id",companyId);
			company = companyMapper.selectOne(wrapper);
		}
		return company;
	}

	@Override
	public int updateCompany(Company company) {
		log.info("[Dubbo] - 被远程调用 -> CompanyHandler -> updateCompany("+company.getCompanyId()+")");
		if (UtilValidate.isEmpty(company)){
			return -1;
		}
		return companyMapper.updateById(company);
	}

	@Override
	public int addCompany(Company company) {
		if (UtilValidate.isEmpty(company)){
			return -1;
		}
		// 随机插入默认的图标
		String defaultLogoPath = "http://localhost/images/defaultConpany/logo-"+ (int) (1 + Math.random() * 10) +".jpg";
		company.setLogo(defaultLogoPath);
		company.setMemberLevel(CompanyEnums.MEMBER_LEVEL.free.getValue());
		company.setCompanyStatus(UserEnums.ACCOUNT_STATUS.normal.getValue());
		company.setMemberEndDate(DateUtil.offsetDay(new Date(), 1));
		return companyMapper.insert(company);
	}
}
